<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_mapQueryByGeometry"></title>
    <style>
      #toolbar {
        position: absolute;
        top: 50px;
        right: 10px;
        text-align: center;
        z-index: 1000;
        border-radius: 4px;
      }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="toolbar" class="panel panel-primary">
  <div class='panel-heading'>
      <h5 class='panel-title text-center' data-i18n="resources.title_queryByGeometry"></h5></div>
  <div class='panel-body content'>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_point" onclick="query('Point')"/>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_line" onclick="query('LineString')"/>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_rectangle" onclick="query('Circle')"/>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_polygon" onclick="query('Polygon')"/>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearFeatures()"/>
  </div>
</div>
<div id="map" style="width: 100%;height:100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
<script type="text/javascript">
    var map, source, resultLayer, draw,
        url = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-world/rest/maps/World";
    map = new ol.Map({
        target: 'map',
        // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
        controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
        view: new ol.View({
            center: [0, 0],
            zoom: 3,
            projection: 'EPSG:4326',
            multiWorld: true
        })
    });
    var layer = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: url
        }),
        projection: 'EPSG:4326'
    });
    source = new ol.source.Vector();
    var vectorLayer = new ol.layer.Vector({
        source: source
    });
    map.addLayer(layer);
    map.addLayer(vectorLayer);

    function query(type) {
      clearFeatures();
      draw = new ol.interaction.Draw({
        source: source,
        type: type,
        geometryFunction: type ==='Circle' && ol.interaction.Draw.createBox()
      });
      map.addInteraction(draw);
      draw.on('drawend', drawCompleted);
    }

    function drawCompleted(e) {
        clearFeatures()
        geo = e.feature.getGeometry();
        var param = new ol.supermap.QueryByGeometryParameters({
            queryParams: {name: "Countries@World"},
            geometry: geo
        });
        new ol.supermap.QueryService(url).queryByGeometry(param).then(function (serviceResult) {
            var vectorSource = new ol.source.Vector({
                features: (new ol.format.GeoJSON()).readFeatures(serviceResult.result.recordsets[0].features),
                wrapX: false
            });
            resultLayer = new ol.layer.Vector({
                source: vectorSource,
                style: new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'red',
                        width: 2
                    }),
                    fill: new ol.style.Fill({
                        color: 'rgba(0, 0, 255, 0.1)'
                    })
                })
            });
            map.addLayer(resultLayer);
        });
    }

    function clearFeatures() { 
      draw && map.removeInteraction(draw);
      map.removeLayer(resultLayer);
      source.clear();
    }
</script>
</body>
</html>